package net.dgg.dggcloud.service.jrcrm.service.log;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.entity.menu.MenuEntity;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.dao.log.SysOpreateModuleRecordDao;
import net.dgg.dggcloud.service.jrcrm.entity.log.SysOpreateModuleRecord;
import net.dgg.dggcloud.service.service.MenuService;

/**
 * 操作模块日志记录Service
 * 
 * @Title: SysOpreateModuleRecordService
 * @Description:
 * @Auther: 雷力
 * @Date: 2018-11-09 14:51:50
 *
 */
@Service
public class SysOpreateModuleRecordService extends BaseService implements SysRecordService {

	@Autowired
	private MenuService menuService;

	@Autowired
	private SysOpreateModuleRecordDao dao;

	@Override
	public void insert(Map<String, Object> map) {
		String uri = map.get("uri").toString();
		List<MenuEntity> menus = menuService.findMenuLikeUrl(uri);
		StringBuffer names = new StringBuffer();
		if (menus.size() > 1) {
			for (MenuEntity menu : menus) {
				StringBuffer temp = new StringBuffer();
				List<MenuEntity> parents = menuService.findParents(menu);
				Iterator<MenuEntity> iterator = parents.iterator();
				while (iterator.hasNext()) {
					temp.append("-" + iterator.next().getName());
				}
				temp.deleteCharAt(0);
				names.append(" / " + temp);
			}
		} else {
			List<MenuEntity> parents = menuService.findParents(menuService.find4Id(menus.get(0).getId()));
			Iterator<MenuEntity> iterator = parents.iterator();
			while (iterator.hasNext()) {
				names.append("-" + iterator.next().getName());
			}
		}
		String str = names.toString().trim();
		map.put("opreate", str.substring(1, str.length()));
		logger.error("=====>"+SysOpreateModuleRecordService.class+"=====>插入数据...");
		dao.insert(map);
	}

	public void insert(User user, String names, String url) {
		SysOpreateModuleRecord record = new SysOpreateModuleRecord();
		record.setId(keyWorker.nextId());
		record.setOpreate(names);
		record.setOpreateTime(new Date());
		record.setUrl(url);
		record.setUserLoginname(user.getLoginName());
		record.setUserName(user.getName());
		dao.saveByHiber(record);
	}

	@SuppressWarnings("rawtypes")
	public List<Map> listOpreateModuleRecordByMap(Map<String, Object> map) {
		return dao.listOpreateModuleRecordByMap(map);
	}

	public Integer countOpreateModuleRecordByMap(Map<String, Object> map) {
		return dao.countOpreateModuleRecordByMap(map);
	}

}
